package com.dongjiantong.quarantine.serviceImpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.biz.service.EarMarkLifeReadeService;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.quarantine.mapper.BreedReportsFileMapper;
import com.dongjiantong.quarantine.mapper.DjtButcherAdmissionInspectionsMapper;
import com.dongjiantong.quarantine.model.DjtButcherAdmissionInspections;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 15/11/17.
 *
 */
@Slf4j
@Service(version = "1.0.0")
public class DjtButcherAdmissionInspectionsReadServiceImpl extends ServiceMybatis<DjtButcherAdmissionInspections> implements DjtButcherAdmissionInspectionsReadService{

    @Resource
    private DjtButcherAdmissionInspectionsMapper djtButcherAdmissionInspectionsMapper;

    @Resource
    private BreedReportsFileMapper breedReportsFileMapper;

    @Reference(version = "1.0.0")
    private EarMarkLifeReadeService earMarkLifeReadeService;

    /**
     * 根据单据ID 查询单据详情
     *
     * @param id 主键ID
     * @return DjtButcherAdmissionInspections
     */
    @Override
    public DjtButcherAdmissionInspections findById(Long id) {
        return djtButcherAdmissionInspectionsMapper.selectByPrimaryKey(id);
    }

    /**
     * 屠宰端的分页查询
     *
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @param map      参数
     * @return PageInfo<DjtButcherAdmissionInspections>
     */
    @Override
    public PageInfo<DjtButcherAdmissionInspections> pagingForButcher(Integer pageNum, Integer pageSize, Map<String, Object> map) {
        PageHelper.startPage(pageNum, pageSize);

        List<DjtButcherAdmissionInspections> djtButcherAdmissionInspectionsList = djtButcherAdmissionInspectionsMapper.selectListForButcher(map);
        for(DjtButcherAdmissionInspections djtButcherAdmissionInspections : djtButcherAdmissionInspectionsList){
            //get files
            map.clear();
            map.put("field","quarantine_cert_photo_list");
            map.put("type1",2);
            map.put("type2",1);
            map.put("relevanceId",djtButcherAdmissionInspections.getId());
            List<String> quarantine_cert_photo_list = breedReportsFileMapper.findListByType(map);
            djtButcherAdmissionInspections.setQuarantine_cert_photo_list(quarantine_cert_photo_list);
            //get ear numbers
            map.clear();
            map.put("reportId",djtButcherAdmissionInspections.getId());
            map.put("reportType",2);
            List<String> ear_tag_numbers = earMarkLifeReadeService.findListByBreedReportId(map);
            djtButcherAdmissionInspections.setEar_tag_numbers(ear_tag_numbers);
            map.put("isSelected",1);
            List<String> remainder_ears = earMarkLifeReadeService.findListByBreedReportId(map);
            djtButcherAdmissionInspections.setRemainder_ears(remainder_ears);
        }

        PageInfo<DjtButcherAdmissionInspections> butcherAdmissionInspectionsPageInfo = new PageInfo<DjtButcherAdmissionInspections>(djtButcherAdmissionInspectionsList);

        return butcherAdmissionInspectionsPageInfo;
    }

    /**
     * 政府端的分页查询
     *
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @param map      参数
     * @return PageInfo<DjtButcherAdmissionInspections>
     */
    @Override
    public PageInfo<DjtButcherAdmissionInspections> pagingForGOV(Integer pageNum, Integer pageSize, Map<String, Object> map) {

        PageHelper.startPage(pageNum, pageSize);

        List<DjtButcherAdmissionInspections> djtButcherAdmissionInspectionsList = djtButcherAdmissionInspectionsMapper.selectListForGOV(map);

        PageInfo<DjtButcherAdmissionInspections> butcherAdmissionInspectionsPageInfo = new PageInfo<DjtButcherAdmissionInspections>(djtButcherAdmissionInspectionsList);

        return butcherAdmissionInspectionsPageInfo;
    }

    /**
     * 获取当前单据状态
     *
     * @param id 单据主键id
     * @return Integer status
     */
    @Override
    public Integer findStatusById(Long id) {
        return djtButcherAdmissionInspectionsMapper.findStatusById(id);
    }

    /**
     * 根据产地检疫的单据id查询入场单据的id
     *
     * @param id 产地检疫的单据id
     * @return Long
     */
    @Override
    public Long checkIsExist(Long id) {
        Map<String , Object> map = Maps.newHashMap();
        map.put("breedId",id);
        return djtButcherAdmissionInspectionsMapper.checkIsExist(map);
    }
}
